﻿/*!	@file	r_window_base.cpp
	@brief	ゲームウィンドウ
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_window_base.h"


cRWindowBase :: cRWindowBase ( void )
	:
	isFullScreen ( false ),
	loopCount ( 0 ),
	execCount ( 0 ),
	isPlaying ( true )
{
}

cRWindowBase :: ~cRWindowBase ( void )
{
}

void	cRWindowBase :: setWindowTitle ( const r_cstr title )
{
}
const r_cstr	cRWindowBase :: getWindoTitle ( void ) const
{
	return NULL;
}


r_ui32	cRWindowBase :: getExecCount ( void ) const
{
	return execCount;
}

r_ui32	cRWindowBase :: getLoopGount ( void ) const
{
	return loopCount;
}

r_bool	cRWindowBase :: checkPlaying ( void ) const
{
	return isPlaying;
}

void*	cRWindowBase :: getWindowhandle ( void ) const
{
	return NULL;
}

void*	cRWindowBase :: getDeviceContext ( void ) const
{
	return NULL;
}

r_bool	cRWindowBase :: getHasFocus ( void ) const
{
	return true;
}

void	cRWindowBase :: getWindwowSize ( r_si32 &width, r_si32 &height ) const
{
	width = windowWidth;
	height = windowHeight;
}



void	cRWindowBase :: setAppName ( const r_cstr name )
{
}

void	cRWindowBase :: setWindowSize ( r_si32 width, r_si32 height )
{
	windowWidth = width;
	windowHeight = height;
}

void	cRWindowBase :: setIsFullScreen ( r_bool fullscreen )
{
	isFullScreen = fullscreen;
}

#include "../draw/r_draw_core.h"
#include "../draw/r_draw_context.h"
#include "../draw/r_draw_swapchain.h"
#include "../timer/r_timer_system.h"
#include "../thread/r_thread_server.h"
#include "../thread/r_thread_profile.h"
#include "../input/r_input_server.h"
#include "../gui/r_gui_server.h"
#include "../resource/r_resource_server.h"
#include "../score/r_score_server.h"

void	cRWindowBase :: loop ( void )
{
	//! 時間のアップデート
	r::timer::TimerSystem::instance	timer;
	timer->update();
	// プロファイル開始
	r::thread::Server::instance		thread;
	thread->begineProfile();

	r::draw::Core::instance			draw;
	r::gui::Server::instance	gui;
	r::input::Server::instance		input;
	r::resource::Server::instance		resource;
	{
		RDEBUG_PROFILE ( _RST ( "draw exec" ), r::thread::profile::Type_Draw );
		// 描画コマンドの実行
		draw->executeCommand();
	}
	{
		RDEBUG_PROFILE ( _RST ( "update" ), r::thread::profile::Type_Default );
		// GUIの更新
		gui->update();

		++loopCount;
		// 同期待ち
		// ゲーム処理を更新
		if ( isPlaying ) {
			++execCount;
			//		isUpdateEnable = false;
			// スコアの更新
			cRScoreServer::instance	score;
			score->update();
		}
		thread->sync();
	}
	{
		RDEBUG_PROFILE ( _RST ( "gui" ), r::thread::profile::Type_Draw );
		gui->draw();
	}
	{
		RDEBUG_PROFILE ( _RST ( "draw mirge" ), r::thread::profile::Type_Draw );
		// 描画コマンドをマージする
		draw->mirgeCommand();
	}
	{
		RDEBUG_PROFILE ( _RST ( "resource" ), r::thread::profile::Type_Default );
		resource->update();
	}
	{
		RDEBUG_PROFILE ( _RST ( "input" ), r::thread::profile::Type_Default );
		// キー入力を取得
		input->update();
	}
	{
		RDEBUG_PROFILE ( _RST ( "swap" ), r::thread::profile::Type_Rest );
		// バッファのスワップ
		draw->getSwapChain()->present();
	}
	thread->endProfile();
}

